#!/usr/bin/env python3
"""
修复客户端tkinter和SSL依赖问题
手动复制必要的DLL文件到客户端目录
"""

import os
import shutil
import sys
from pathlib import Path

def fix_tkinter_dependencies(client_dir):
    """修复tkinter和SSL依赖"""
    print("=" * 50)
    print("修复客户端tkinter和SSL依赖")
    print("=" * 50)
    
    client_path = Path(client_dir)
    if not client_path.exists():
        print(f"❌ 客户端目录不存在: {client_dir}")
        return False
    
    # 获取Python安装路径
    python_path = Path(sys.executable)
    python_dir = python_path.parent
    
    # 可能的DLL位置
    dll_locations = [
        python_dir / "DLLs",
        python_dir.parent / "DLLs",
        Path("E:/AGI_Class/anaconda3/DLLs"),
        Path("E:/AGI_Class/anaconda3/Library/bin"),
        Path("E:/AGI_Class/anaconda3"),
    ]
    
    # 需要的DLL文件
    required_dlls = [
        # tkinter相关
        "_tkinter.pyd",
        "tcl86t.dll", 
        "tk86t.dll",
        "ffi.dll",
        
        # SSL相关
        "_ssl.pyd",
        "libssl-3-x64.dll",
        "libcrypto-3-x64.dll",
        
        # 其他网络和系统相关
        "_socket.pyd",
        "_hashlib.pyd",
        "select.pyd",
        "unicodedata.pyd",
        "_lzma.pyd",
        "_bz2.pyd",
        "pyexpat.pyd",
        "_ctypes.pyd",
        "_sqlite3.pyd",
        
        # 可能需要的其他DLL
        "zlib1.dll",
        "libffi-8.dll",
        "sqlite3.dll",
        "LIBBZ2.dll",
        "liblzma.dll",
        "libexpat.dll",
    ]
    
    # TCL库目录
    tcl_locations = [
        python_dir.parent / "tcl",
        Path("E:/AGI_Class/anaconda3/tcl"),
        Path("E:/AGI_Class/anaconda3/Library/lib"),
    ]
    
    print(f"客户端目录: {client_path}")
    print(f"Python目录: {python_dir}")
    
    # 复制DLL文件
    copied_count = 0
    for dll_name in required_dlls:
        dll_found = False
        for dll_location in dll_locations:
            dll_file = dll_location / dll_name
            if dll_file.exists():
                target_file = client_path / dll_name
                try:
                    shutil.copy2(dll_file, target_file)
                    print(f"✓ 复制DLL: {dll_name}")
                    dll_found = True
                    copied_count += 1
                    break
                except Exception as e:
                    print(f"⚠️  复制失败 {dll_name}: {e}")
        
        if not dll_found:
            print(f"⚠️  未找到DLL: {dll_name}")
    
    # 复制TCL库
    tcl_found = False
    for tcl_location in tcl_locations:
        if tcl_location.exists():
            tcl_target = client_path / "tcl"
            try:
                if tcl_target.exists():
                    shutil.rmtree(tcl_target)
                shutil.copytree(tcl_location, tcl_target)
                print(f"✓ 复制TCL库: {tcl_location}")
                tcl_found = True
                break
            except Exception as e:
                print(f"⚠️  复制TCL库失败: {e}")
    
    if not tcl_found:
        print("⚠️  未找到TCL库目录")
    
    print("=" * 50)
    print(f"修复完成！复制了 {copied_count} 个DLL文件")
    return True

def main():
    """主函数"""
    import sys
    
    if len(sys.argv) != 2:
        print("使用方法: python fix_tkinter_client.py <客户端目录>")
        print("示例: python fix_tkinter_client.py WMS_Client_OneFile_v2.1.0_20250626_102855")
        return
    
    client_dir = sys.argv[1]
    fix_tkinter_dependencies(client_dir)

if __name__ == "__main__":
    main() 